// pages/malltaskPackage/pages/mallDetail/mallDetail.js
const app = getApp();
const quote = app.globalData.quote;
const path = quote.paths;
const util = require('../../../../utils/util.js');
const tools = require('../../../../utils/tools.js');
const api = require('../../../../utils/api.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    commodityList: [],
    commodityInfo: null,
    imgUrl: null,
    buy: true,
    buy_2: false,
    confirmBuy: false,
    phoneSuccess: false,
    telphone: null,
    code: null,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that = this
    console.log(options, '22')
    console.log('列表页点击传入的数据', JSON.parse(options.commodity))
    var commodityInfo = JSON.parse(options.commodity).data
    that.setData({
      imgUrl: app.globalData.imgUrl,
      commodityInfo: commodityInfo,
      commodityList: commodityInfo.CommodityImageList
    })
  },
  onShow: function() {

  },
  //普通商品点击购买
  buy: function() {
    var that = this
    wx.showToast({
      title: '非vip', //提示文字
      duration: 2000, //显示时长
      mask: false, //是否显示透明蒙层，防止触摸穿透，默认：false  
      icon: 'loading', //图标，支持"success"、"loading"  

    })

  },
  //vip用户购买
  cantBuy: function(e) {
    var that = this
    console.log(e)

    var money = e.currentTarget.dataset.money

    if (money > 10) {
      that.setData({
        confirmBuy: true,
        buy_2: true,
        buy: false,
      })

    } else {
      wx.showToast({
        title: '金币不足', //提示文字
        duration: 2000, //显示时长
        mask: false, //是否显示透明蒙层，防止触摸穿透，默认：false  
        icon: 'loading', //图标，支持"success"、"loading"  

      })
    }
  },
  //获取用户手机号码
  phoneNumInput: function(e) {
    var val = e.detail.value;
    this.setData({
      telphone: val
    });
  },
  //提交用户手机号码
  // getphone:function(e){
  //   var that = this 
  //   let dataDic = {
  //     Phone: that.data.telphone
  //   } ;
  //   api.methodEnglishCorner_Recording({
  //     dataDic,
  //     success:function(res){
  //       console.log(res.data,'126查看是否提交成功')
  //       that.setData({
  //         code:res.data.code
  //       })
  //       if(res.data.code == -8 ){

  //       wx.showModal({
  //         title: '提示',
  //         content: '手机号码已提交过',
  //         showCancel: false,//是否显示取消按钮
  //         success:function(res){
  //           if (res.cancel){

  //           }
  //         }
  //       })
  //       }
  //     }
  //   })
  // },
  // let dataDic = {
  //   CommodityId: e.currentTarget.dataset.id,
  //   UCUID: 10015,
  //   Phone: that.data.telphone
  // }
  //         wx.showLoading({
  //   title: '生成订单中',
  // })
  //         api.methodEnglishCorner_CommodityTransaction({
  //   dataDic,
  //   success: function (res) {
  //     console.log(res.data, '122')
  //     wx.hideLoading()
  //     if (res.data.code != 0) {
  //       wx.showToast({
  //         title: '操作失败',
  //         icon: 'loading', //图标，支持"success"、"loading"  
  //         duration: 1500
  //       })
  //       return
  //     }
  //     that.setData({
  //       paySuccess: true,
  //       phoneSuccess: false,
  //       redeemCode: res.data.data
  //     })
  //     that.saveRedeemCode(res.data.data)
  //   }
  // })
  //课程用户支付
  payconfirm: function(e) {
    var that = this
    if (!that.data.telphone) {
      wx.showModal({
        title: '提示',
        content: '手机号不能为空',
        showCancel: false,
      })
      return
    }
    let dataDic = {
      CommodityId: e.currentTarget.dataset.id,
      UCUID: app.globalData.UCUID,
      Phone: that.data.telphone
    }
    wx.showLoading({
      title: '生成订单中',
    })
    api.methodEnglishCorner_CommodityTransaction({
      dataDic,
      success: function(res) {
        console.log(res.data, '122')
        wx.hideLoading()
        if (res.data.code == -3) {
          wx.showModal({
            title: '提示',
            content: res.data.msg,
            showCancel: false,//是否显示取消按钮
            success: function (res) {
              if (res.cancel) {

              }
            }
          })
          that.setData({
            phoneSuccess: false
          })
          return
        }
        that.setData({
          paySuccess: true,
          phoneSuccess: false,
          redeemCode: res.data.data
        })
        that.saveRedeemCode(res.data.data)
      }
    })
  },
  //非课程用户确定支付
  confirmBuy: function(e) {
    var course = e.currentTarget.dataset.course
    var that = this
    if (course == 0) {
      console.log('不是课程类商品')
      let dataDic = {
        CommodityId: e.currentTarget.dataset.id,
        UCUID: app.globalData.UCUID,
      }
      wx.showLoading({
        title: '生成订单中',
      })
      api.methodEnglishCorner_CommodityTransaction({
        dataDic,
        success: function(res) {
          console.log(res.data, '148')
          wx.hideLoading()
          if (res.data.code != 0) {
            wx.showToast({
              title: '操作失败',
              icon: 'loading', //图标，支持"success"、"loading"  
              duration: 1500
            })
            return
          }
          that.setData({
            paySuccess: true,
            redeemCode: res.data.data
          })
          that.saveRedeemCode(res.data.data)
        }
      })
    } else if (course == 1) {
      console.log('是课程类商品')
      that.setData({
        phoneSuccess: true,
      })
    }
  },

  // 复制商品兑换码
  saveRedeemCode(code) {
    wx.setClipboardData({
      data: code,
      success: (res) => {
        wx.getClipboardData({
          success: (res) => {
            console.log(res)
            wx.showToast({
              title: '兑换码已复制',
              icon: 'success',
              duration: 1000
            })
          }
        })
      }
    })
  },








  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})